﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
namespace HHHTool
{
    //字符串补充方法
    public class StringSupplement
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }
        /// <summary>
        /// 移除数字
        /// </summary>
        /// <param name="needDeal"></param>
        /// <returns></returns>
        public static string RemoveNumber(string needDeal)
        {
            string strDealed1 =Regex.Replace(needDeal, "[0-9]", "");
            return strDealed1;
        }
        /// <summary>
        /// 移除字母
        /// </summary>
        /// <param name="needDeal"></param>
        /// <returns></returns>
        public static string RemoveLetter(string needDeal) {
            string strRemoved = Regex.Replace(needDeal, "[a-z]","", RegexOptions.IgnoreCase);
            return strRemoved;
        }
        /// <summary>
        /// 移除汉字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveChineseWord(string str)
        {
            string retValue = Regex.Replace(str, "[\u4e00-\u9fa5]", "");

            return retValue;
        }

        //  sArray=st.split("_");  字符串更加特点的字符  进行分组
        //sArray = str.Split(new char[2] { 'j', '_' }); 是利用多个字符进行字符串分组
        //sArray = str.Split( new string[]{"Ji","jB"}, StringSplitOptions.RemoveEmptyEntries); 根据字符串或字符串组来截取字符串
        /*提取字符串中的第i个字符开始的长度为j的字符串； 
        string str = "GTAZB_JiangjBen_123";
        int start = 3, length = 8;
        Console.WriteLine(str.Substring(start-1, length));
        */
        /*删除字符串中的特定字符串Jiangj
            string str = "GTAZB_JiangjBen_123";
            string tSt;
            tSt = str.Replace("Jiangj","");
         */
        /// <summary>
        /// 设置一个文章类型的字符串  将一个字符串进行换行计算  并进行分页计算
        /// </summary>
        /// <param name="str"></param>
        /// 
        #region 继承自mono的字符串 处理方法
        public List<string> SetStr(string str,int lineMaxNum)
        {
            List<string> returnStrs = new List<string>();
            Debug.Log("字符串测试：" + str);
            str = str.Replace("\n", "").Replace(" ", "").Replace("　", "").Replace("\t", "").Replace("\r", "");
            float lineIndex = 0;
            float indexInLine = 0;
            int subIndex = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (lineIndex >= 4)
                {
                    Debug.Log(subIndex);
                    var printText = str.Substring((subIndex - 1) < 0 ? 0 : (subIndex - 1), i - subIndex - 1);
                    returnStrs.Add(printText);
                    lineIndex = 0;
                    subIndex = i;
                }
                else
                {

                    if (i >= str.Length - 1)
                    {
                        var printText = str.Substring((subIndex - 1) < 0 ? 0 : (subIndex - 1));
                        returnStrs.Add(printText);
                    }
                }
                string ch = str[i].ToString();
                string strSub = (str.Length - i) >= 4 ? str.Substring(i, 4) : str.Substring(i, str.Length - 1 - i);
                if (i > 0)
                {
                    //test str

                    if (ch == "［")
                    {
                        str = str.Insert(i, "\n");
                        Debug.Log(string.Format("》》english括号：{0}：{1}。", i, strSub));
                        lineIndex++;
                        indexInLine = 0;
                        i++;
                        continue;
                    }
                    if (ch == "【")
                    {
                        str = str.Insert(i, "\n");
                        Debug.Log(string.Format("》》》》chinese括号：{0}：{1}。", i, strSub));
                        lineIndex++;
                        indexInLine = 0;
                        i++;
                        continue;
                    }

                }

                if (indexInLine > lineMaxNum)
                { 
                    if (char.IsPunctuation(char.Parse(ch))) {
                        str = str.Insert(i + 1, "\n");
                        Debug.Log(string.Format("》》》》》》》》number标点换行：{0}。{1}", i, strSub));
                        i++;
                    }
                    //if (ch == "," || ch == "，" || ch == "。" || ch == "." || ch == ";")
                    //{
                    //    str = str.Insert(i + 1, "\n");
                    //    Debug.Log(string.Format("》》》》》》》》number标点换行：{0}。{1}", i, strSub));
                    //    i++;
                    //}
                    else { lineIndex++; }
                      
                    indexInLine = 0;
                }
                indexInLine++;

            }
            return returnStrs;
        }

        /// <summary>
        /// 根据文本内容和每行显示最大值返回一个数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="lineMaxNum"></param>
        /// <returns></returns>
        public static List<string> StrMaker(string str,int lineMaxNum) {
          
            List<string> returnStr = new List<string>();
            str = str.Trim();
            StringBuilder stringBuilder = new StringBuilder(str) ;
            bool workingWithText = true;
            while (workingWithText) 
            {
                if (stringBuilder.Length <= lineMaxNum)
                {
                    returnStr.Add(stringBuilder.ToString());
                    workingWithText = false;
                    continue;
                }
                //Debug.Log(stringBuilder[lineMaxNum].ToString());
                if (isPunctuation(stringBuilder[lineMaxNum].ToString()))
                {
                    returnStr.Add(stringBuilder.ToString(0,lineMaxNum + 1));
                    stringBuilder.Remove(0, lineMaxNum + 1);
                }
                else {
                    returnStr.Add(stringBuilder.ToString(0,lineMaxNum));
                    stringBuilder.Remove(0, lineMaxNum );
                }               
            }
           

            return returnStr;
        }

        #endregion
        /// <summary>
        /// 是不是中文标点符号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isCnPunctuation(string str) {
            Regex regex = new Regex(@"[\，\。\？\！\；\：]");

            if (regex.IsMatch(str) ) {
                return true;
            }
            return false;

        }
        /// <summary>
        /// 判断是不是英文标点符号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isEnPunctuation(string str) {
            Regex regex = new Regex(@"[\,\.\?\;\!\:]");

            if (regex.IsMatch(str))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断是不是中英文标点符号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isPunctuation(string str) {
            if (!isCnPunctuation(str)) return false;
            if (!isEnPunctuation(str)) return false;
            return true;
        }
    }
}
